package com.neusoft.elmboot.controller;

import java.util.List;

import com.neusoft.elmboot.service.ex.DataNotFoundException;
import com.neusoft.elmboot.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.neusoft.elmboot.po.Business;
import com.neusoft.elmboot.service.BusinessService;

@RestController
@RequestMapping("/businesses")
public class BusinessController {
	public static final int OK = 200;
	public static final int DATA_NOT_FOUND = 204;

	@Autowired
	private BusinessService businessService;

	//根据食物类型查询商家信息
	@GetMapping("/orderType/{orderTypeId}")
	public JsonResult<List<Business>> listBusinessByOrderTypeId(@PathVariable Integer orderTypeId) {
		JsonResult<List<Business>> result = new JsonResult<List<Business>>();
		try {
			List<Business> data = businessService.listBusinessByOrderTypeId(orderTypeId);
			result = new JsonResult<List<Business>>(OK, data);
			result.setMessage("获取商家列表成功");
			return result;
		} catch (DataNotFoundException e) {
			// 处理异常
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	//根据商家id查询商家信息
	@GetMapping("/{businessId}")
	public JsonResult<Business> getBusinessById(@PathVariable Integer businessId) {
		JsonResult<Business> result = new JsonResult<Business>();
		try {
			Business data = businessService.getBusinessById(businessId);
			result = new JsonResult<Business>(OK, data);
			result.setMessage("获取商家信息成功");
			return result;
		} catch (DataNotFoundException e) {
			// 处理异常
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}

	@GetMapping("/search/{keyword}")
	public JsonResult<List<Business>> searchBusinesses(@PathVariable String keyword) {
		JsonResult<List<Business>> result = new JsonResult<List<Business>>();
		try {
			List<Business> data = businessService.searchBusinesses(keyword);
			result = new JsonResult<List<Business>>(OK, data);
			result.setMessage("搜索商家成功");
			return result;
		} catch (DataNotFoundException e) {
			// 处理异常
			result.setState(DATA_NOT_FOUND);
			result.setMessage(e.getMessage());
			return result;
		}
	}
}
